﻿using System;
using System.Linq;
using Ones.ServiceAdvertisementManagement;

namespace Ones.WebAdmin
{
    public partial class AdsMgmtContactPage : System.Web.UI.Page
    {
        private ServiceAdvertisementManagementClient c = null;
        private AdvertisementContact contact = null;
        private int itemId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["cmd"] != null)
                {
                    fillData();

                    if (Session["cmd"].ToString().Equals("delete"))
                    {
                        txtName.Enabled = false;
                        txtCompany.Enabled = false;
                        txtPhone.Enabled = false;
                    }
                }
            }
        }

        public void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                contact = new AdvertisementContact();

                contact.advContName = txtName.Text;
                contact.advContCompany = txtCompany.Text;
                contact.advContPhone = txtPhone.Text;

                if (c.addAdvertisementContact(contact))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListContactPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                contact = new AdvertisementContact();

                contact.advContId = int.Parse(Session["id"].ToString());
                contact.advContName = txtName.Text;
                contact.advContCompany = txtCompany.Text;
                contact.advContPhone = txtPhone.Text;

                if (c.updateAdvertisementContact(contact))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListContactPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                itemId = int.Parse(Session["id"].ToString());

                if (c.deteleAdvertisementContact(itemId))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListContactPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("cmd");
            Response.Redirect("~/WebAdmin/AdsListContactPage.aspx");
        }

        private void fillData()
        {
            c = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");

            itemId = int.Parse(Session["id"].ToString());
            contact = c.searchAdvertisementContactById(itemId);

            txtName.Text = contact.advContName;
            txtCompany.Text = contact.advContCompany;
            txtPhone.Text = contact.advContPhone;

            c.Close();
        }
    }
}